package umls.interfaces.pubmed.server;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.log4j.PropertyConfigurator;
import org.fbk.it.hlt.jlsi.DomainTermSimilarity;
import org.fbk.it.hlt.jlsi.Percentili;
import org.fbk.it.hlt.jlsi.ScoreTermMap;
import org.fbk.it.hlt.jlsi.TermNotFoundException;
import org.fbk.it.hlt.jlsi.data.TextCorpusReader;


public class Svdlibc {
	private static Runtime envir;
	public Svdlibc(){
		envir = Runtime.getRuntime();
	}
	public static void genSparseTable(File output_dir) throws IOException {
		String logConfig = "MedServiceData/log-config.txt";
		new File(output_dir.getAbsoluteFile() + "_matrix/").mkdirs();
		String output = output_dir.getAbsoluteFile() + "_matrix/X";
		File stop = new File("MedServiceData/stop-words.txt");
		long begin = System.currentTimeMillis();

		PropertyConfigurator.configure(logConfig);

		TextCorpusReader tcr = new TextCorpusReader(output_dir, output, stop,
				100);
		int expectedAutovalues = tcr.columnCount / 10;
		if (expectedAutovalues > 100)
			expectedAutovalues = 100;
		if (expectedAutovalues < 2)
			expectedAutovalues = 2;
		long end = System.currentTimeMillis();
		System.out.println("corpus readDocumentList in " + (end - begin)
				+ " ms\nStarting svd");

		Process p = null;
		//System.out.println("Launching MedServiceData/SVDLIBC/svd -v 0 -d " + expectedAutovalues + " -o "
		//		+ output + " -r sb " + output + "-matrix");
		p = envir.exec("MedServiceData/SVDLIBC/svd -v 0 -d " + expectedAutovalues + " -o "
				+ output + " -r sb " + output + "-matrix");

		BufferedInputStream br = new BufferedInputStream(p.getInputStream());
		byte[] autovalori = new byte[10];
		int size = br.read(autovalori);
		StringBuilder result = new StringBuilder();
		for (int i = 0; i < size - 1; i++) {
			result.append((char) autovalori[i]);
		}

		int autovalues = Integer.parseInt(result.toString());
		p.destroy();
		// String result = new String(autovalori);
		if (expectedAutovalues > autovalues) {
			System.out
			.println("Numero di autovalori minore del previsto, rilancio programma");
			p = envir.exec("SVDLIBC/svd -v 0 -d " + autovalues + " -o "
					+ output + " -r sb " + output + "-matrix");
			System.out.println("SVDLIBC/svd -v 0 -d " + autovalues + " -o "
					+ output + " -r sb " + output + "-matrix");
		} else
			System.out
			.println("Numero di autovalori desiderato sufficiente, eseguito");

	}
	public static void getDomainTermSimilarity(String term, String dir)
	throws IOException {
		File Ut = new File(dir + "X-Ut");
		File Sk = new File(dir + "X-S");
		File r = new File(dir + "X-row");
		File c = new File(dir + "X-col");
		File df = new File(dir + "X-df");
		File f = new File(dir + term + "_similarity.txt");
		BufferedWriter output = new BufferedWriter(new FileWriter(f));
		DomainTermSimilarity sim = new DomainTermSimilarity(Ut, Sk, r, c, df,
				-1, 100000);
		Percentili p[]= null;
		try {
			ScoreTermMap map = sim.compareAll(term);
			output.write(map.toString());
			p= map.toPercentili();
			for (int i = 0; i < p.length; i++) {
				if (p[i].contains("angiogenesis"))
					System.out.println("angiogenesis trovato nel percentile "
							+ (100 - i));
				if (p[i].contains("vegfa gene"))
					System.out.println("vegfa gene trovato nel percentile "
							+ (100 - i));
				// System.out.println("Percentile "+i+" dimensione "+test[i].getSize());
			}
		} catch (TermNotFoundException e) {

			e.printStackTrace();
		}
		output.flush();
		output.close();
		Runtime runt = Runtime.getRuntime();
		runt.gc();
		//return p;
		return;
	}
}
